//
// Created by gavin on 2016/11/3.
//

#ifndef DSCREENPUSH_DZLOG_H
#define DSCREENPUSH_DZLOG_H
#include <android/log.h>

#define DEBUG_LOG 1

#undef LOGV
#undef LOGD
#undef LOGI
#undef LOGW
#undef LOGE

//#define AWLOG(level, fmt, arg...)
//        LOG_PRI(level, LOG_TAG, "<%s:%u>: "fmt, strrchr(__FILE__, '/')+1, __LINE__, ##arg)

// (0 - 5)
#define LOG_AFLAG 4


#if (LOG_AFLAG == 0)
#elif (LOG_AFLAG == 1)
#define D_LOGE
#elif (LOG_AFLAG == 2)
#define D_LOGE
#define D_LOGW
#elif (LOG_AFLAG == 3)
#define D_LOGE
#define D_LOGW
#define D_LOGD
#elif (LOG_AFLAG == 4)
#define D_LOGE
#define D_LOGW
#define D_LOGD
#define D_LOGI
#elif (LOG_AFLAG == 5)
#define D_LOGE
#define D_LOGW
#define D_LOGD
#define D_LOGI
#define D_LOGV
#else
#endif



#ifdef DEBUG_LOG

#ifdef D_LOGV
#define LOGV(fmt, arg...)   do { \
		__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, "<line[%04d] %s()> " fmt, __LINE__, __FUNCTION__, ##arg); \
	} while(0)
#else
#define LOGV(...)
#endif

#ifdef D_LOGD
#define LOGD(fmt, arg...)   do { \
		__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "<line[%04d] %s()> " fmt, __LINE__, __FUNCTION__, ##arg); \
	} while(0)
#else
#define LOGD(...)
#endif

#ifdef D_LOGI
#define LOGI(fmt, arg...)   do { \
		__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "<line[%04d] %s()> " fmt, __LINE__, __FUNCTION__, ##arg); \
	} while(0)
#else
#define LOGI(...)
#endif

#ifdef D_LOGW
#define LOGW(fmt, arg...)   do { \
		__android_log_print(ANDROID_LOG_WARN, LOG_TAG, "<line[%04d] %s()> " fmt, __LINE__, __FUNCTION__, ##arg); \
	} while(0)
#else
#define LOGW(...)
#endif
#ifdef D_LOGE
#define LOGE(fmt, arg...)   do { \
		__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "<line[%04d] %s()> " fmt, __LINE__, __FUNCTION__, ##arg); \
	} while(0)
#else
#define LOGE(...)
#endif
#else
#define LOGV(...)
#define LOGD(...)
#define LOGI(...)
#define LOGW(...)
#define LOGE(...)
#endif

// performance debug
#define DBG_TIME_ENABLE		0
#if DBG_TIME_ENABLE
#include <time.h>
#define LOG_TIME			LOGD
#define DBG_TIME_BEGIN(inf, en)											\
	int64_t llt_ms = systemTime() / 1000000;							\
	int64_t lt_ms = llt_ms;												\
	int64_t nt_ms = 0;													\
	if (en)	LOG_TIME("\t[T_DBG_BGN] %s timestamp: %lld", inf, lt_ms);

#define DBG_TIME_DIFF(inf, cnt)												\
	nt_ms = systemTime() / 1000000;										\
	if(nt_ms -lt_ms >= cnt)LOG_TIME("\t[T_DBG_DFF] %s use: %lld(ms)", inf, (nt_ms - lt_ms));	\
	lt_ms = nt_ms;

#define DBG_TIME_END(inf, en)											\
	nt_ms = systemTime() / 1000000;										\
	if(nt_ms - llt_ms >= en)LOG_TIME("\t[T_DBG_end] %s use: %lld(ms)", inf, (nt_ms - llt_ms));
/* if (en) LOG_TIME("\t[T_DBG_END] %s timestamp: %lld", inf, nt_ms); */
#else
#define DBG_TIME_BEGIN(inf, en) {}
#define DBG_TIME_DIFF(inf, cnt) {}
#define DBG_TIME_END(inf, en) {}
#endif


#define DBG_ONE_SEC_FRAME_ENABLE		0
#if DBG_ONE_SEC_FRAME_ENABLE
#define LOG_FRAME			LOGW
#define DBG_ONE_SEC_FRAME(inf,id)											\
{                                                                           \
    static int frameCount[2] = {0, 0};                                      \
    static time_t last[2] = {time(NULL), time(NULL)};                       \
	time_t now = time(NULL);                                                \
	if (now - last[id] >= 1) {                                              \
        LOG_FRAME("%s  %d frameCount = %d\n",inf, id,frameCount[id]+1);     \
        frameCount[id] = 0;                                                 \
        last[id] = time(NULL);                                              \
    } else {                                                                \
        frameCount[id] ++;                                                  \
    }                                                                       \
}
#else
#define DBG_ONE_SEC_FRAME(inf,id)  {}
#endif
#endif //DSCREENPUSH_DZLOG_H
